package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.mapper.PmsSkuStockMapper;
import com.itheima.pojo.PmsSkuStock;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

@DubboService
public class PmsSkuStockImpl implements PmsSkuStockApi {

    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;

    /**
     * 批量更新sku库存信息
     */
    @Override
    public void update(Integer pid, List<PmsSkuStock> skuStockList) {
        LambdaQueryWrapper<PmsSkuStock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,pid);
        List<PmsSkuStock> pmsSkuStocks = pmsSkuStockMapper.selectList(lqw);
        for (PmsSkuStock pmsSkuStock : pmsSkuStocks) {
            for (PmsSkuStock skuStock : skuStockList) {
                if (Objects.equals(pmsSkuStock.getId(), skuStock.getId())) {
                    pmsSkuStockMapper.updateById(skuStock);
                }
            }
        }
    }

    /**
     * 根据商品id以及sku编码模糊查询sku库存
     */
    @Override
    public List<PmsSkuStock> findByPid(Integer pid, String keyword) {

        LambdaQueryWrapper<PmsSkuStock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,pid).like(ObjectUtils.isNotEmpty(keyword),PmsSkuStock::getSkuCode,keyword);
        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockMapper.selectList(lqw);
        return pmsSkuStockList;

    }

    /**
     *商品添加
     */
    @Override
    public void save(List<PmsSkuStock> skuStockList, Long productId) {
        for (PmsSkuStock pmsSkuStock : skuStockList) {
            pmsSkuStock.setProductId(productId);
            pmsSkuStockMapper.insert(pmsSkuStock);
        }
    }

    /**
     *根据商品id获取商品编辑信息
     */
    @Override
    public List<PmsSkuStock> updateInfo(Integer productId) {
        LambdaQueryWrapper<PmsSkuStock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,productId);
        List<PmsSkuStock> pmsSkuStockList = pmsSkuStockMapper.selectList(lqw);
        return pmsSkuStockList;
    }

    /**
     *更新商品
     */
    @Override
    public void updateProduct(List<PmsSkuStock> skuStockList, Long productId) {
        for (PmsSkuStock pmsSkuStock : skuStockList) {
            pmsSkuStock.setProductId(productId);
            pmsSkuStockMapper.updateById(pmsSkuStock);
        }
    }


}
